package com.camel.weixin.service.impl;

import cn.hutool.json.JSONObject;
import com.camel.core.config.StringConstants;
import com.camel.core.exceptions.CustomException;
import com.camel.core.service.AbstractBaseCommonService;
import com.camel.core.utils.HttpUtils;
import com.camel.core.utils.ResultUtil;
import com.camel.weixin.mapper.WxAccountMapper;
import com.camel.weixin.model.WxAccount;
import com.camel.weixin.service.WxAccountService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *
 * @author Administrator
 */
@Service
public class WxAccountServiceImpl extends AbstractBaseCommonService<WxAccountMapper, WxAccount> implements WxAccountService<WxAccount> {
    @Override
    public JSONObject refreshToken(Integer id) {
        WxAccount wxAccount = getById(id);
        JSONObject json = HttpUtils.get(true, StringConstants.WEIXIN_DOMAIN,
                StringConstants.WEIXIN_TOKEN_PATH + "?" + StringConstants.GRANT_TYPE_STR + "="
                        + StringConstants.CLIENT_CREDENTIAL_STR + "&" + StringConstants.APPID_STR + "="
                        + wxAccount.getAppid() + "&" + StringConstants.SECRET_STR + "="
                        + wxAccount.getAppSecret());
        if (json.containsKey(StringConstants.ACCESS_TOKEN)) {
            wxAccount.setAccessToken(json.getStr(StringConstants.ACCESS_TOKEN));
            wxAccount.setTokenTime(new Date());
            updateById(wxAccount);
            return json;
        }
        throw new CustomException(json.getStr(StringConstants.WEIXIN_ERRMSG_STR));
    }
}




